var app = getApp();
Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userInfo: {},
    hasUserInfo: false,
    highername: '',
  },
  //获取用户登录记录判断如果有就提取 并不显现登录 没有就
  onLoad: function(options) {
    if (app.globalData.userInfo == null) {
      hasUserInfo: false
    }
    else {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    }
  },

  bindGetUserInfo: function(e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      wx.setStorageSync('userInfo', e.detail.userInfo)
      app.globalData.userInfo = {
        'userInfo': e.detail.userInfo,
      }
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
      })
      this.login();
      // 授权成功后，跳转进入小程序首页
      wx.switchTab({
        url: '/pages/index/index'
      })
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  login: function() {
    let that = this;
    let token = wx.getStorageSync('token');
    if (token) {
      wx.request({
        url: app.globalData.hostname + ":" + app.globalData.port +'/user/check-token',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: {
          token: token
        },
        success: function(res) {
          if (res.data.code != 0) {
            wx.removeStorageSync('token')
            that.login();
          } else {
            wx.getUserInfo({
              success: res => {
                that.setData({
                  userInfo: res.userInfo,
                  hasUserInfo: true,
                })
                app.globalData.userInfo = res.userInfo
                app.globalData.wxid = wx.getStorageSync('wxid')
              }
            })
          }
        },
      })
      return;
    }
    wx.login({
      success: function(res) {
        wx.request({
          url: app.globalData.hostname + ":" + app.globalData.port +'/user/wxapp/login',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: {
            code: res.code
          },
          success: function(res) {
            if (res.data.code == 10000) {
              // 去注册
              that.registerUser();
              return;
            }
            if (res.data.code != 0) {
              // 登录错误
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: '无法登录，请重试',
                showCancel: false
              })
              return;
            }
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('wxid', res.data.userId)
            app.globalData.wxid = res.data.userId
            //回到原来的地方放
            // wx.navigateBack();
            that.login()
          }
        })
      }
    })
  },
  registerUser: function() {
    var that = this;
    wx.login({
      success: function(res) {
        var code = res.code; // 微信登录接口返回的 code 参数，下面注册接口需要用到
        wx.getUserInfo({
          success: function(res) {
            var iv = res.iv;
            var encryptedData = res.encryptedData;
            // 下面开始调用注册接口
            wx.request({
              url: app.globalData.hostname + ":" + app.globalData.port +'/user/wxapp/register/complex',
              header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
              },
              data: {
                code: code,
                encryptedData: encryptedData,
                iv: iv
              }, // 设置请求的 参数
              success: (res) => {
                wx.hideLoading();
                that.login();
              }
            })
          }
        })
      }
    })
  },

  go_cart: function() {
    wx.switchTab({
      url: '/pages/product_cart/product_cart'
    })
  },
  go_us: function() {
    wx.navigateTo({
      url: '/pages/about_us/about_us'
    })
  },
})